import { dynamicRoutes, router } from '@/router'
import { nestedRouteHandle, getCacheWhiteList } from '@/utils/permission'


/**
 * 此处是通用模板，除非有特殊需求不然不推荐修改。
 */


const state = () => {
    return {
        token: null,
        userInfo: {},
        roleSign: null,
        permsisions: [],
        sidebarList: [],
    }
}

const mutations = {
    loadRoute(state) {
        // 处理路由与侧边栏
        let { routes, sidebars } = nestedRouteHandle(dynamicRoutes)
        let cacheViews = getCacheWhiteList(routes).join(',')
        this.commit('appConfig/setCacheViews', cacheViews)

        routes.forEach(element => {
            router.addRoute(element)
            router.options.routes.push(element)
        });
        state.sidebarList = sidebars
    }, setUserInfo(state, { token, userInfo, permsisions, roleSign }) {
        state.token = token
        state.userInfo = userInfo
        state.permsisions = permsisions
        state.roleSign = roleSign
        this.commit('user/loadRoute')
    },
    outLogin(state) {
        state.token = null
        state.userInfo = {}
        state.roleSign = null
        state.permsisions = []
        state.sidebarList = []
    }
}
const actions = {
    setUserInfo(context, { token, userInfo, permsisions, roleSign }) {
        context.commit('setUserInfo', { token, userInfo, permsisions, roleSign })
    },
    outLogin(context) {
        context.commit('outLogin')
    }

}
const getters = {

}


export default {
    namespaced: true,
    state,
    mutations,
    actions,
    getters
}